package cn.linran.org.service.impl;

import cn.linran.basic.exception.BusinessException;
import cn.linran.basic.service.impl.BaseServiceImpl;
import cn.linran.basic.util.BaiduAuditUtils;
import cn.linran.org.domain.Employee;
import cn.linran.org.domain.Shop;
import cn.linran.org.domain.ShopAuditLog;
import cn.linran.org.dto.ShopDto;
import cn.linran.org.mapper.EmployeeMapper;
import cn.linran.org.mapper.ShopAuditLogMapper;
import cn.linran.org.mapper.ShopMapper;
import cn.linran.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.mail.javamail.JavaMailSender;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlemnt(Shop shop) {
        //1、检验商铺名字是否为空值
        if(StringUtils.isEmpty(shop.getName())
                ||StringUtils.isEmpty(shop.getAddress())
                ||StringUtils.isEmpty(shop.getTel())

                ||StringUtils.isEmpty(shop.getAdmin().getUsername())
                ||StringUtils.isEmpty(shop.getAdmin().getPhone())
                ||StringUtils.isEmpty(shop.getAdmin().getEmail())
                ||StringUtils.isEmpty(shop.getAdmin().getPassword())
                ||StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            throw new BusinessException("参数不能为空");//如果为空则抛出异常
        }
        //1.11、通过AI审核名称是否违规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){
            throw new BusinessException("该店铺名称违规了额！！");
        }
        //1.12、判断两次密码是否一直
        Employee admin = shop.getAdmin();
        if(!admin.getPassword().equals(admin.getComfirmPassword()))
        {
            throw new BusinessException("两次密码输入不一致,重新输入密码！");
        }
        //1.2、判断店铺是否已经被入驻？（店铺名+地址）
        Shop dbShop = shopMapper.getShopByMessage(shop);//重新写autowired
        if(dbShop!=null){
            throw new BusinessException("该店铺已经被注册！");
        }

        //2、以上都通过则把shop和employee数据保存在表中
        //2.1、保存employee,获取自增主键
        employeeMapper.save(admin);
        //2.2、给shop中的admin_id赋值
        shop.setAdmin_id(admin.getId());
        //2.3、保存shop,获取自增主键
        shopMapper.save(shop);
        //2.4、将admin中的shop_id赋值
        admin.setShop_id(shop.getId());
        //2.5、更新admin
        employeeMapper.update(admin);

    }

    /**
     * 商店审核通过
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        // 1. 修改shop状态
        // 通过shop_id 查询shop数据  ，再修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        // 审核通过
        shop.setState(2);
        shopMapper.update(shop);

        // 2. 录入日志  shopLog
        log.setState(2);
        log.setAuditId(1L);
        shopAuditLogMapper.save(log);

        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("791034807@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);

        // 获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("791034807@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 商店审核驳回
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        // 1. 修改shop状态
        // 通过shop_id 查询shop数据  ，再修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        System.out.println("商店名称："+shop.getId());
        // 审核通过
        shop.setState(4);
        shopMapper.update(shop);

        // 2. 录入日志  shopLog
        log.setState(4);
        log.setAuditId(1L);
        shopAuditLogMapper.save(log);

        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("791034807@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);

        // 获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("791034807@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);

        /**
         * 两种情况:
         *    1. 修改状态为驳回  state = 4; 下次入驻的时候判断该店铺是否是被驳回过，如果是就直接继续入驻。
         *    2. 直接删除 shop,  emp
         *
         */
    }

    /**
     * 返回echarts数据
     * @return
     */
    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }

}
















